


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;


/*@Sangeeta
* @Uses:1. ngram Calculation needs divsion by file name or path name
* 2. @Added...................opt_similarity function within itself. 
* 3. @Table Created: h1_score
* 
* @SET
* 1. SCORE TABLE
* 2. Check optimality function
*/
public class h2_opt 
{
	
 public Connection conn =  null;
 public String dbname="2jboss";
 public String userid="root";
 public String passwd = "123";
 public ArrayList<String> bugids =  null;
 public String score_table = "h1_score";

 
 public void process(Connection conn)
 {
	 System.out.println("I am in h1_opt_2");
	 bugids =  new ArrayList<String>();
	 String bug_q= "SELECT distinct bugid FROM bug_version where version in (select version from selected_version_for_4)";
	  	 
	 Statement stmt =  null;
	 ResultSet rs =  null;
	 
	 try
	  {
		  stmt = conn.createStatement();
	      stmt.execute(bug_q);
	      rs = stmt.getResultSet();
	    
	      while(rs.next())
	       {
		     String bugid =  rs.getString("bugid");
		     bugids.add(bugid);
		 
	       }//wh
	      
	      if(rs!=null)
	    	  rs.close();
	      if(stmt!=null)
	    	  stmt.close();
	 
	 } catch (SQLException e) 
	  { e.printStackTrace();	}
	 
	 //bugid, title, description, version
	 for(int i=56;i<bugids.size();i++)
	 {
		 System.out.println("count="+(i+1));
		 
		 String bugid = bugids.get(i);
		 String title = "";
		 String desc="";
		 String version="";
		 String td_q = "select * from bug_svn_info where bugid='"+bugid+"'";
		 String v_q = "select version from bug_version where bugid='"+bugid+"'";
		
		 try 
		  {
			 stmt=conn.createStatement();
			 stmt.execute(td_q);
			 rs = stmt.getResultSet();
			 while(rs.next())
			 	{
				 	title = rs.getString("summary");
				 	desc =  rs.getString("description");
			 	}
		 
			 stmt.execute(v_q);
			 rs=stmt.getResultSet();
			 
			 while(rs.next())
			 {
				 version = rs.getString("version");
				 boolean valid =isValidVesion(version,conn); 
				 if(valid)
				 {
					 break;
				 }
			 }//wh
		 
			 if(rs!=null)
					rs.close();
				 
			     if(stmt!=null)
				   stmt.close(); 
			 
		  }catch(SQLException e)
		  {
			  e.printStackTrace();
		  }
		  
		  System.out.println("bugid="+bugid+" version="+version);
		 // System.out.println("title="+title);
		 // System.out.println("desc="+desc);
		 
		  calculate_and_insert(bugid,title,desc,version);
	 }//for
	 	
	 try
	 {
	     if(rs!=null)
			rs.close();
		 
	     if(stmt!=null)
		   stmt.close();
	 
 	} catch (SQLException e) 
 	{ e.printStackTrace(); 	}

 }//process
 
//calculate the ngram match between this bug report title and desc
private void calculate_and_insert(String bugid, String title, String desc,String version)
{

	Connection conn =  null;
	db_conn db =  new db_conn();
	conn =  db.connect("2jboss", "root", "123");
		 
	 String old_title = title;
	 String old_desc  = desc;
	 
	 general_fun gf =  new general_fun();
	 
	 title = gf.clean(title);
	 title = gf.remove_stop_words(title);
	 title = gf.remove_SW(title);
	 
	 desc = gf.clean(desc);
	 desc = gf.remove_stop_words(desc);
	 desc = gf.remove_SW(desc);
	 
	 String title_ngram[]= gf.ngram(title, 3, 10);
	 String desc_ngram[] = gf.ngram(desc, 3, 10);
	 
	 ngram_match ng=  new ngram_match();
	 
	 file_extraction fe =  new file_extraction();
	 
	 String formated_version = formated_version(version);
	 System.out.println("version="+version+ "  formated(table)="+formated_version);
	 String table =  formated_version;
	 
	 Statement stmt = null;
	 String str_q= "select * from  " + table;
	 try 
	 {
		stmt =  conn.createStatement();
		stmt.execute(str_q);
		ResultSet  rs = stmt.getResultSet();
		
		ArrayList<td_fpath_object> td =  new ArrayList<td_fpath_object>();
	
		
		int count = 0;
		while(rs.next())
		 {
			System.out.println("count="+count++);
			String fid =rs.getString("file_id");
			String file_name = rs.getString("file_name");
			int file_no = rs.getInt("file_no");
			String file_path =rs.getString("file_path");
			//System.out.println("File Path="+file_path);
			
			file_name =  file_name.toLowerCase();
			file_name = file_name.replace(".java", "");
			String file_name_ngram[] =gf.ngram(file_name, 3, 10);
			
			file_path =file_path.toLowerCase();
			file_path=gf.remove_slash_and_file_name(file_path);
			file_path = file_path.replace(".java","");
			int first_space_index = file_path.indexOf(" ");
			file_path =  file_path.substring(first_space_index);// removing first String from the file path "JBOSS_AS_4_0_3"
			String file_path_ngram[] = gf.ngram(file_path, 3, 10);
			
			 //System.out.println("file  name="+file_name+" file_path="+file_path);
			
			 double tf_sim = opt_similarity(title_ngram,file_name_ngram);
			 double tpath_sim =opt_similarity(title_ngram, file_path_ngram);
			 double df_sim = opt_similarity(desc_ngram, file_name_ngram);
			 double dpath_sim=opt_similarity(desc_ngram, file_path_ngram);
			 
			 td.add(new td_fpath_object(bugid,fid,table,title,desc,file_name,file_path,tf_sim,tpath_sim,df_sim,dpath_sim));
			 //d.add(new td_fpath_object(bugid,fid,table,desc,file_name,file_path,df_sim,dpath_sim));
			
			// insert_to_t_sim(bugid,fid,table,title,file_name,file_path,tf_sim,tpath_sim);//title
			 //insert_to_d_sim(bugid,fid,table,desc,file_name,file_path,df_sim,dpath_sim);//similarity
			// intr();
			
		 }//while
		
		if(rs!=null)
			rs.close();
		if(stmt!=null)
			stmt.close();
		
		gf.insert_to_td_sim(td,conn,score_table);
	
		if(conn!=null)
			conn.close();
		
		System.out.println("bugid="+bugid+ "  formated(table)="+formated_version);
	
		
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
}//cal and insert


//after applying loop  optimization
public double opt_similarity(String[] aNgrams, String[] bNgrams) 
{
	double result = 0;
	int alen = aNgrams.length;
	int blen = bNgrams.length;
	
    int score= 0;
	for(int i=alen ;--i>=0;)
	{
		String x = aNgrams[i];

		for(int j=blen;--j>=0;)
			{
				String y = bNgrams[j];

					if(x.equalsIgnoreCase(y))
					{
					   int new_score = x.length(); 	
				       score = score+new_score;	    
						//result = result + 1;
					}
					
					else if(y.length()<x.length())
					{
						break;
					}
			}
	}//for

	result =  score;// should be changed[score function]**	
/*	if(alen>0 && blen>0)
	{
	 //result = ((score*1.0)/(alen*blen))*10000;//@@ This formula is giving 0 because lenthg of title Ngram is very large
		result = ((score*1.0)/blen);
	}
		*/
	return result;
	
}

/*
private void insert_to_td_sim(ArrayList<td_fpath_object>td) 
{
    Statement stmt =  null;
    
    for(int i=0;i<td.size();i++)
    {
      
    	String bugid = td.get(i).bugid;
    	String fid = td.get(i).fid;
    	String table = td.get(i).table;
    	String title = td.get(i).title;
    	String desc =  td.get(i).desc;    	
    	String file_name = td.get(i).file_name;
    	String file_path= td.get(i).file_path;
    	double tf_sim =td.get(i).tf_sim;
    	double tpath_sim = td.get(i).t_path_sim;
    	double df_sim =td.get(i).df_sim;
    	double dpath_sim = td.get(i).d_path_sim;
    	
    	double sum=tf_sim+tpath_sim+df_sim+dpath_sim;
    	//double sum = tf_sim+tpath_sim;
    	
    	
    	String insert_q = "insert into h1_score values('"+bugid+"','"+fid+"','"+table+"','"+title+"','"+desc+"','"+
    	file_name+"','"+file_path+"',"+tf_sim+","+tpath_sim+","+df_sim+","+dpath_sim+","+sum+")";
        
    	try 
    		{
    			stmt =  conn.createStatement();
    			stmt.executeUpdate(insert_q);
		
    		} catch (SQLException e) 
    			{	e.printStackTrace();}
    }//for
	
    try
    {
	 if(stmt!=null)
		stmt.close();
    }catch(SQLException e)
    {e.printStackTrace();}
	
}*/

public String formated_version(String version)
{
	 String formated_version="";

	 version =  version.toLowerCase();
	 version =  version.replace("jbossas-", "");
	 version =  version.replaceAll("final","").trim();	
	 version =  version.replaceAll("\\.", "_");
	 version = version.replaceAll("\\s+", "_");
	 
	// System.out.println("version = "+version); 
	 
	 int  i =-1;
	 int index= -1;
	 for(i=0;i<version.length();i++)
	  {
		 char a = version.charAt(i);
		 if(a=='_')
			 continue;
		 
		 else if(((a-'0')>=0)&&((a-'0')<=9))
		 {
			 index = i;
			 continue;
		 }
		 else
		 {
			 break;
		 }

	 }
	 
	 
	 //System.out.println("index="+index);
	 formated_version = version.substring(0, index+1);
	 formated_version =  formated_version.concat("_");
	 
	 //System.out.println("fm1="+formated_version);	 
	// System.out.println("v len="+version.length());
	 
	 if(index!=(version.length()-1))
	 {
	  formated_version =  formated_version.concat(version.substring(index+1));
	 }
	 
	 //System.out.println("fm="+formated_version);
	 formated_version =  formated_version.replaceAll("__", "_");
	 
	 if(formated_version.endsWith("_"))
	 {
		 int u_index = formated_version.lastIndexOf("_");
		 formated_version = formated_version.substring(0, u_index);
	 }	
		
		
  return formated_version;
}

public boolean isValidVesion(String version, Connection conn) 
{
    Statement stmt =  null;
    String v_q="select version from  selected_version_for_4";
    Vector<String> selected_versions =  new Vector<String>();
    
    try
     {
    	stmt = conn.createStatement();
		stmt.execute(v_q);
		ResultSet rs =  stmt.getResultSet();
		
		while(rs.next())
		 {
		  selected_versions.add(rs.getString("version")); 	
		 }
		
		if(rs!=null)
			rs.close();
		if(stmt!=null)
			stmt.close();
	 } catch (SQLException e)
	 { 		e.printStackTrace(); 	}    
	 
	 //for
     boolean flag =  false;
	 for(int i=0;i<selected_versions.size();i++)
	 {
		 if(version.equalsIgnoreCase(selected_versions.get(i)))
		 {
			  flag=  true;
		 }//if
	 }
	 
	return flag; 
	
}//isValidVesion


public void intr()
 {
	 BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
	 System.out.println("INTR");
	 try {
		br.readLine();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
 }
 
public static void main(String args[])
{
		h2_opt obj =  new h2_opt();
		db_conn db=  new db_conn();
		obj.conn = db.connect(obj.dbname, obj.userid, obj.passwd);
		obj.process(obj.conn);
		db.close(obj.conn);
     		
 }//main

}//class
